package com.bjgov.scoresys.util;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.hibernate.Session;

import com.bjgov.scoresys.dao.HibernateSessionFactory;

public class HibernateFilter implements Filter {

	public void destroy() {
	}

	public void doFilter(ServletRequest servletRequest,
			ServletResponse servletResponse, FilterChain filterChain)
			throws IOException, ServletException {
		try {
			filterChain.doFilter(servletRequest, servletResponse);
		} finally {
			Session session = HibernateSessionFactory.getSession();
			if (session != null) {
				if (session.isOpen()) {
					session.flush();
				}
			}
			HibernateSessionFactory.closeSession();
		}
	}

	public void init(FilterConfig filterConfig) throws ServletException {
	}

}
